// DlgEditHTML.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgEditHTML.h"
#include "afxdialogex.h"

#define TIMER_ID 1001


// CDlgEditHTML dialog

IMPLEMENT_DYNAMIC(CDlgEditHTML, CDialog)

CDlgEditHTML::CDlgEditHTML(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgEditHTML::IDD, pParent)
	, m_bUserListed(FALSE)
{
	m_nType = -1;
}

CDlgEditHTML::~CDlgEditHTML()
{
}

void CDlgEditHTML::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_SIZE, m_ctrlSize);
	DDX_Check(pDX, IDC_CHECK_ENABLE, m_bUserListed);
	DDX_Control(pDX, IDC_BUTTON_BOLD, m_btnBold);
	DDX_Control(pDX, IDC_BUTTON_ITALIC, m_btnItalic);
	DDX_Control(pDX, IDC_BUTTON_COLOR, m_btnColor);
	DDX_Control(pDX, IDC_BUTTON_UNDO, m_btnUndo);
	DDX_Control(pDX, IDC_BUTTON_BREAK, m_btnBreak);
}


BEGIN_MESSAGE_MAP(CDlgEditHTML, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_BOLD, &CDlgEditHTML::OnBnClickedButtonBold)
	ON_BN_CLICKED(IDC_BUTTON_ITALIC, &CDlgEditHTML::OnBnClickedButtonItalic)
	ON_BN_CLICKED(IDC_BUTTON_COLOR, &CDlgEditHTML::OnBnClickedButtonColor)
	ON_CBN_SELCHANGE(IDC_COMBO_SIZE, &CDlgEditHTML::OnCbnSelchangeComboSize)
	ON_BN_CLICKED(IDC_BUTTON_BREAK, &CDlgEditHTML::OnBnClickedButtonBreak)
	ON_BN_CLICKED(IDC_BUTTON_APPLY, &CDlgEditHTML::OnBnClickedButtonApply)
	ON_WM_SIZE()
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CDlgEditHTML message handlers

BOOL CDlgEditHTML::OnInitDialog()
{
	CDialog::OnInitDialog();

	// shouldn't happen but check it anyway
	if( m_nType < 0 )
		EndDialog( 0 );

	// create html edit
	if( !m_ctrlEditHTML.CreateFromStatic( IDC_STATIC_HTML_EDIT, this ))
		EndDialog( -100 );

	// set buttons
	HICON hIcon = NULL;
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_BOLD), IMAGE_ICON, 16, 16, 0);
	m_btnBold.SetIcon( hIcon );
	m_btnBold.SetTooltip(_T("Bold highlighted text"));
	
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_ITALIC), IMAGE_ICON, 16, 16, 0);
	m_btnItalic.SetIcon( hIcon );
	m_btnItalic.SetTooltip(_T("Italicize highlighted text"));
	
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_COLOR), IMAGE_ICON, 16, 16, 0);
	m_btnColor.SetIcon( hIcon );
	m_btnColor.SetTooltip(_T("Set color for highlighted text"));
	
	hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON_UNDO), IMAGE_ICON, 16, 16, 0);
	m_btnUndo.SetIcon( hIcon );
	m_btnUndo.SetTooltip(_T("Undo last"));

	m_btnBreak.SetTooltip(_T("Add line break"));

	// add sizes
	m_ctrlSize.AddString( _T("Font Size") );
	m_ctrlSize.AddString( _T("1 (8 pt)") );
	m_ctrlSize.AddString( _T("2 (10 pt)") );
	m_ctrlSize.AddString( _T("3 (12 pt)") );
	m_ctrlSize.AddString( _T("4 (14 pt)") );
	m_ctrlSize.AddString( _T("5 (18 pt)") );
	m_ctrlSize.AddString( _T("6 (24 pt)") );
	m_ctrlSize.AddString( _T("7 (36 pt)") );	
	// set data
	m_ctrlSize.SetItemData( 0, 3 );
	m_ctrlSize.SetItemData( 1, 1 );
	m_ctrlSize.SetItemData( 2, 2 );
	m_ctrlSize.SetItemData( 3, 3 );
	m_ctrlSize.SetItemData( 4, 4 );
	m_ctrlSize.SetItemData( 5, 5 );
	m_ctrlSize.SetItemData( 6, 6 );
	m_ctrlSize.SetItemData( 7, 7 );
	m_ctrlSize.SetItemData( 8, 8 );
	// set defualt
	m_ctrlSize.SetCurSel( 0 );

	// enable enable
	if( m_nType == INFORMATION_NEWS_ITEM_MACRO )
	{
		// get information
		appConfig.m_SQL.GetInformation(m_nType, &m_szHTML, &m_bUserListed);
		// enable checkbox
		GetDlgItem( IDC_CHECK_ENABLE )->ShowWindow( SW_SHOW );
	}

	// start timer
	SetTimer( TIMER_ID, 100, NULL );

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CDlgEditHTML::OnOK()
{
	// update data
	UpdateData();

	// get HTML
	m_ctrlEditHTML.GetDocumentHTML( m_szHTML );

	// set information
	appConfig.m_SQL.SetInformation(m_nType, m_szHTML, m_bUserListed);

	CDialog::OnOK();
}


void CDlgEditHTML::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	CDialog::OnCancel();
}


void CDlgEditHTML::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
}


void CDlgEditHTML::OnTimer(UINT_PTR nIDEvent)
{
	if( nIDEvent == TIMER_ID )
	{
		KillTimer( TIMER_ID );
		if( !m_szHTML.IsEmpty() )
			m_ctrlEditHTML.SetDocumentHTML( m_szHTML );

		m_ctrlEditHTML.ExecCommand( IDM_CARETBROWSINGMODE, OLECMDEXECOPT_DODEFAULT );
		m_ctrlEditHTML.SetFocus();
	}

	CDialog::OnTimer(nIDEvent);
}


void CDlgEditHTML::OnBnClickedButtonApply()
{
	// update data
	UpdateData();

	// get HTML
	m_ctrlEditHTML.GetDocumentHTML( m_szHTML );

	// set information
	appConfig.m_SQL.SetInformation(m_nType, m_szHTML, m_bUserListed);
}


void CDlgEditHTML::OnBnClickedButtonBold()
{
	// set bold
	m_ctrlEditHTML.Bold();
	m_ctrlEditHTML.SetFocus();
}


void CDlgEditHTML::OnBnClickedButtonItalic()
{
	// set italic
	m_ctrlEditHTML.Italic();
	m_ctrlEditHTML.SetFocus();
}


void CDlgEditHTML::OnBnClickedButtonColor()
{
	// get color and set it
	CColorDialog dlg;
	if( dlg.DoModal() == IDOK )
	{
		COLORREF cColor = dlg.GetColor();
		m_ctrlEditHTML.SetForeColor( cColor );
		m_ctrlEditHTML.SetFocus();
	}
}


void CDlgEditHTML::OnCbnSelchangeComboSize()
{
	UINT nSize = (UINT)m_ctrlSize.GetItemData( m_ctrlSize.GetCurSel() );
	m_ctrlEditHTML.SetFontSize( nSize );
	m_ctrlSize.SetCurSel( 0 );
}


void CDlgEditHTML::OnBnClickedButtonBreak()
{
	m_ctrlEditHTML.ExecCommand( IDM_LINEBREAKNORMAL, OLECMDEXECOPT_DODEFAULT, NULL, NULL );
}
